package com.example.BlockChain.service.impl;

import com.example.BlockChain.dao.ProductDao;
import com.example.BlockChain.dto.ProductSummaryDTO;
import com.example.BlockChain.entity.TProduct;
import com.example.BlockChain.service.ProductService;
import com.example.BlockChain.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;
    @Override
    public List<TProduct> getAllGoods(int userid){ return productDao.queryProductByUserid(userid);}

    @Override
    public List<ProductSummaryDTO> getProductsByMerchantId(Integer merchantId) {
        if (merchantId == null || merchantId <= 0) {
            return Collections.emptyList(); // 返回空列表避免 NPE
        }
        return productDao.getProductsByMerchantId(merchantId);
    }

    @Override
    public void addProduct(TProduct product) {
        productDao.save(product); // 调用 Dao 层逻辑
    }

    @Override
    public boolean editProduct(TProduct product) {
        // 调用 Mapper 层方法执行更新
        int rowsAffected = productDao.updateProduct(product);
        return rowsAffected > 0; // 判断是否更新成功
    }

    @Override
    public boolean deleteProduct(Long productId) {
        // 调用 DAO 层的删除方法
        int rowsAffected = productDao.deleteProduct(productId);

        // 如果影响的行数大于 0，说明删除成功，返回 true；否则返回 false
        return rowsAffected > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductsByIds(List<Long> productIds) {
        if (productIds == null || productIds.isEmpty()) {
            throw new IllegalArgumentException("商品ID列表不能为空");
        }
        // 调用 DAO 层的批量删除方法
        productDao.deleteByIds(productIds);
    }

}
